package it.polimi.traveldream.beans;

import it.polimi.traveldream.dto.GiftListElementDTO;
import it.polimi.traveldream.dto.PaidDTO;
import it.polimi.traveldream.ejb.CustomerManager;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "giftListBean")
@ViewScoped
public class GiftListBean {

	@EJB
	private CustomerManager manager;

	private List<GiftListElementDTO> giftElements;

	private Integer currentId;

	@PostConstruct
	public void init() {
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap();
		String user = params.get("userId");
		if (user != null && !user.isEmpty()) {
			this.currentId = Integer.valueOf(user);
		} else {
			this.currentId = this.manager.getCurrentId();
		}
		this.giftElements = this.manager.getGiftList(this.currentId);
	}

	public List<GiftListElementDTO> getGiftElements() {
		return this.giftElements;
	}

	public void setGiftElements(List<GiftListElementDTO> giftElements) {
		this.giftElements = giftElements;
	}

	public String pay(GiftListElementDTO element, PaidDTO paid) {
		paid.setQuantity(element.getCartPackage().getQuantity());
		this.manager.doGift(paid);
		return null;
	}

	public Integer getCurrentId() {
		return this.currentId;
	}

	public void setCurrentId(Integer currentId) {
		this.currentId = currentId;
	}
}
